<template>
  <div class="adduser">
    <div class="adduser_box">
      <el-form
        :model="userInfo"
        status-icon
        :rules="rules"
        ref="userInfo"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model.number="userInfo.userName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="userInfo.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input
            type="password"
            v-model="userInfo.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="用户身份">
          <el-select v-model="formInline.region" placeholder="用户身份">
            <el-option v-for="(item , index) in identity" :key="index" :label="item.identity_text" :value="item.identity_id" ></el-option>
            <!-- <el-option label="出题者" value="uf81yn-hv8uvv"></el-option>
            <el-option label="浏览者" value="zi0gu7-v7dy08"></el-option> -->
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('userInfo')"
            >提交</el-button
          >
          <el-button @click="resetForm('userInfo')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { REGISTER_USER , GET_IDENTITY } from '../../../store/actionType' 
import { createNamespacedHelpers } from 'vuex'
let { mapActions , mapState } = createNamespacedHelpers('user')
export default {
  data() {
    return {
      userInfo: {
        pass: "",
        checkPass: "",
        userName: "",
      },
      formInline: {
        region: "",
      },
      rules: {
        pass: [{ validator: this.validatePass, trigger: "blur" }],
        checkPass: [{ validator: this.validatePass2, trigger: "blur" }],
        userName: [{ validator: this.validateUserName, trigger: "blur" }],
      },
    };
  },

  computed:{
    ...mapState(['identity']),
    // getidentity(){
    //   return 
    // }
  },

  methods: {
    ...mapActions([REGISTER_USER , GET_IDENTITY]),
    validateUserName(rule, value, callback) {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      } else {
        callback();
      }
    },
    validatePass(rule, value, callback) {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.userInfo.checkPass !== "") {
          this.$refs.userInfo.validateField("checkPass");
        }
        callback();
      }
    },
    validatePass2(rule, value, callback) {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.userInfo.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // console.log(this.formInline.region);
          // alert("submit!");
          this[REGISTER_USER]({
            user_name:this.userInfo.userName,
            user_pwd:this.userInfo.pass,
            identity_id:this.formInline.region
          }).then(res=>{
            if (res) {
              this.$message({
                message: "用户添加成功",
                type: "success",
              });
              this.userInfo.pass = ''
              this.userInfo.checkPass = ''
              this.userInfo.userName = ''
              this.formInline.region = ''
            }else{
                console.log(res);
                this.$message({
                message: "用户添加失败",
                type: "error",
              });
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
    mounted() {
    this[GET_IDENTITY]()
  },
};
</script>

<style lang="scss" scoped>
.adduser_box {
  width: 300px;
}
</style>